

function say() {
    alert("LC:637. 二叉树的层平均值");
}
say();
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var averageOfLevels = function(root) {
    //11:14-29
    var levelArr = []
    var resArr = []
    var rootArr = [root, null]

    while(rootArr.length){
        var first = rootArr.shift()
        if(first){
            levelArr.push(first.val)
            if(first.left) {
                rootArr.push(first.left)
            }
            if(first.right){
                rootArr.push(first.right)
            }
        }else {
            resArr.push(arrayAverage(levelArr))
            levelArr = []
            if(rootArr.length) {
                rootArr.push(null)
            }
        }
    }
    return resArr
};

function arrayAverage(arr) {
    var sum = 0
    var len = arr.length
    for(var i = 0; i < len; i++){
        sum += arr[i]
    }
    return sum / len
}
